//括号匹配 链栈实现
#include<stdio.h>
#include<malloc.h>

typedef struct StackNode
{
    char data;
    struct StackNode *next;
}StackNode,*LinkStack;

int main()
{
    LinkStack head;
    head=(LinkStack)malloc(sizeof(StackNode));
    head->next=NULL;

    int Push(LinkStack top,char e);
    int Pop(LinkStack head);
    char GetTop(LinkStack head);

    char temp,compare;
    scanf("%1c",&temp);
    while(temp!=EOF)
    {
        if(temp=='(' || temp=='[' || temp=='{'  || temp=='<')  Push(head,temp);
        else
        {
            if(temp==')')
            {
                compare=GetTop(head);
                if(compare=='(')     Pop(head);
                else
                {
                    printf("匹配失败\n");
                    break;
                }
            }
            else if(temp=='}')
            {
                compare=GetTop(head);
                if(compare=='{')     Pop(head);
                else
                {
                    printf("匹配失败\n");
                    break;
                }
            }
            else if(temp==']')
            {
                compare=GetTop(head);
                if(compare=='[')     Pop(head);
                else
                {
                    printf("匹配失败\n");
                    break;
                }
            }
            else if(temp=='>')
            {
                compare=GetTop(head);
                if(compare=='<')     Pop(head);
                else
                {
                    printf("匹配失败\n");
                    break;
                }
            }
            else if(temp=='\n')  continue;
            else
            {
                printf("输入错误\n");
                break;
            }
        }
        scanf("%1c",&temp);
    }
}


int Push(LinkStack head,char e)
{
    LinkStack p;
    p=(LinkStack)malloc(sizeof(StackNode));
    p->data=e;
    p->next=head->next;
    head->next=p;
    printf("入栈成功%c\n",e);
}

int Pop(LinkStack head)
{
    char e;
    LinkStack p;
    p=head->next;
    e=p->data;
    head->next=p->next;
    printf("出栈成功%c\n",e);
}

char GetTop(LinkStack head)
{
    char e;
    e=head->next->data;
    return(e);
}
